﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class Province : Repository, IBll.IProvince
    {
        private IDao.IProvinceDAO provinceDAO = new Dao.ProvinceDAO();
        public Model.Common.JsonResult Add(DTO.ProvinceDTO t)
        {
            if (t == null)
            {
                return new Model.Common.JsonResult()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull
                };
            }

            if (!string.IsNullOrEmpty(t.Name))
            {
                if (provinceDAO.GetByName(t.Name) != null)
                {
                    return new Model.Common.JsonResult()
                    {
                        LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists,
                        ErrorMessage = "省份名称已经存在"
                    };
                }
            }
            if (!string.IsNullOrEmpty(t.Code))
            {
                if (provinceDAO.GetByName(t.Code) != null)
                {
                    return new Model.Common.JsonResult()
                    {
                        LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists,
                        ErrorMessage = "省份编码已经存在"
                    };
                }
            }
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            Model.Entities.Model_Province model_Province = new Model.Entities.Model_Province();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Province);
            if (string.IsNullOrEmpty(model_Province.ID) || model_Province.ID.Trim() == "")
            {
                model_Province.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = provinceDAO.Add(model_Province);
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(DTO.ProvinceDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            Model.Entities.Model_Province model_Province = new Model.Entities.Model_Province();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Province);
            jsonResult.Success = provinceDAO.Delete(model_Province);
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = provinceDAO.Delete(id);
            return jsonResult;
        }

        public Model.Common.JsonResult Update(DTO.ProvinceDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            Model.Entities.Model_Province model_Province = new Model.Entities.Model_Province();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Province);
            jsonResult.Success = provinceDAO.Update(model_Province);
            return jsonResult;
        }

        public DTO.ProvinceDTO GetByID(string id)
        {
            Model.Entities.Model_Province model_Province = new Model.Entities.Model_Province();
            DTO.ProvinceDTO provinceDTO = new DTO.ProvinceDTO();
            model_Province = provinceDAO.GetByID(id);
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Province, provinceDTO);
            return provinceDTO;
        }

        public DTO.ProvinceDTO GetByName(string provinceName)
        {
            Model.Entities.Model_Province model_Province = new Model.Entities.Model_Province();
            DTO.ProvinceDTO provinceDTO = new DTO.ProvinceDTO();
            model_Province = provinceDAO.GetByName(provinceName);
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Province, provinceDTO);
            return provinceDTO;
        }

        public DTO.ProvinceDTO GetByCode(string provinceCode)
        {
            Model.Entities.Model_Province model_Province = new Model.Entities.Model_Province();
            DTO.ProvinceDTO provinceDTO = new DTO.ProvinceDTO();
            model_Province = provinceDAO.GetByCode(provinceCode);
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Province, provinceDTO);
            return provinceDTO;
        }


        public List<DTO.ProvinceDTO> GetByCountryID(string countryID)
        {
            List<Model.Entities.Model_Province> listModelProvince = new List<Model.Entities.Model_Province>();
            listModelProvince = provinceDAO.GetProvincesByCountryID(countryID);
            List<DTO.ProvinceDTO> listProvinceDTO = new List<DTO.ProvinceDTO>();
            IBll.ICountry country = new Bll.Country();
            foreach (Model.Entities.Model_Province item in listModelProvince)
            {
                DTO.ProvinceDTO provinceDTO = new DTO.ProvinceDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, provinceDTO);
                if (listProvinceDTO != null)
                {
                    DTO.CountryDTO countryDTO = new DTO.CountryDTO();
                    countryDTO = country.GetByID(provinceDTO.CountryID);
                    if (countryDTO != null)
                    {
                        provinceDTO.CountryName = countryDTO.CountryName;
                    }
                }
                listProvinceDTO.Add(provinceDTO);
            }
            return listProvinceDTO;
        }
    }
}
